package com.caimiao.miao.library.database.table;

import android.content.ContentValues;
import android.database.Cursor;

import com.caimiao.miao.bean.TicketOut;
import com.caimiao.miao.library.database.DBHelperImpl;

import java.util.ArrayList;
import java.util.List;

//出库
public class TicketOutDBHelper extends DBHelperImpl<TicketOut> {

    @Override
    protected String getTableName() {
        return TicketOutTable.TABLE_NAME;
    }

    public TicketOutDBHelper() {
        super();
    }

    public boolean hasTrade(String tradeNo) {
        return has(TicketOutTable.ATTR_TRADE + " = ?", new String[]{tradeNo});
    }

    public synchronized int updateStateByTrade(String trade, int state) {
        ContentValues values = new ContentValues();
        values.put(TicketOutTable.ATTR_RESULT, state);
        return update(values, TicketOutTable.ATTR_TRADE + " = ?", new String[]{String.valueOf(trade)});
    }

    public synchronized int updateSync(TicketOut ticket) {
        ContentValues values = new ContentValues();
        values.put(TicketOutTable.ATTR_SYNC, ticket.getSync());
        return update(values, TicketOutTable.ATTR_TRADE + " = ?", new String[]{ticket.getOut_trade_no()});
    }

    public List<TicketOut> getNotSyncData() {
        List<TicketOut> dataList = new ArrayList<>();
        Cursor cursor = db.query(getTableName(), null,
                TicketOutTable.ATTR_SYNC + " = ?", new String[]{"0"}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                dataList.add(get(cursor));
            }
            cursor.close();
        }
        return dataList;
    }

    @Override
    public TicketOut get(Cursor cursor) {
        TicketOut data = new TicketOut();
        data.setId(cursor.getInt(cursor.getColumnIndex(TicketInTable._ID)));
        data.setType(cursor.getString(cursor.getColumnIndex(TicketOutTable.ATTR_TYPE)));
        data.setTicket_num(cursor.getString(cursor.getColumnIndex(TicketOutTable.ATTR_COUNT)));
        data.setDate(cursor.getString(cursor.getColumnIndex(TicketOutTable.ATTR_DATE)));
        data.setNickname(cursor.getString(cursor.getColumnIndex(TicketOutTable.ATTR_USER)));
        data.setOut_trade_no(cursor.getString(cursor.getColumnIndex(TicketOutTable.ATTR_TRADE)));
        data.setState(cursor.getInt(cursor.getColumnIndex(TicketOutTable.ATTR_RESULT)));
        data.setSync(cursor.getInt(cursor.getColumnIndex(TicketOutTable.ATTR_SYNC)));
        return data;
    }

    @Override
    public ContentValues get(TicketOut data) {
        ContentValues values = new ContentValues();
        values.put(TicketOutTable.ATTR_TYPE, data.getType());
        values.put(TicketOutTable.ATTR_COUNT, data.getTicket_num());
        values.put(TicketOutTable.ATTR_DATE, data.getDate());
        values.put(TicketOutTable.ATTR_USER, data.getNickname());
        values.put(TicketOutTable.ATTR_TRADE, data.getOut_trade_no());
        values.put(TicketOutTable.ATTR_RESULT, data.getState());
        values.put(TicketOutTable.ATTR_SYNC, data.getSync());
        return values;
    }
}
